﻿using System;
using System.Web.Mvc;
using Microsoft.Web.Mvc;
using SapaFinance.DTO;
using SapaFinance.Services;

using Telerik.Web.Mvc;

namespace SapaFinance.UI.Controllers
{
    public class AgentController : Controller
    {
        private readonly IAgentService _agentService;

        public AgentController(IAgentService agentService)
        {
            _agentService = agentService;
        }


        public virtual ActionResult RenderAgent(Guid id)
        {
            return View("EditorTemplates/AgentList", _agentService.GetSelectedListById(id));
        }
        public virtual ActionResult Index()
        {
            return View("Index", _agentService.GetListDTO());
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Insert(AgentDTO dto)
        {
            if (ModelState.IsValid){
                _agentService.Insert(dto);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();

        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Delete(Guid id)
        {
            _agentService.Delete(id);
            return this.RedirectToAction(x => x.Index());
        }

        [HttpPost]
        [GridAction]
        public virtual ActionResult Update(AgentDTO dto)
        {
            if (ModelState.IsValid){
                _agentService.Update(dto);
                return this.RedirectToAction(x => x.Index());
            }
            return Index();
        }
    }
}